﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
    public class FeeDAL : BaseDAL
    {
        /// <summary>
        /// Lấy hêt dữ liệu trong bảng tb_Fee
        /// </summary>
        /// <returns>DataTable</returns>
        public DataTable GetAllData()
        {
            try
            {
                base.Open();
                base.Cmd.CommandText = "select * from tb_Fee";
                SqlDataReader reader = base.Cmd.ExecuteReader();
                DataTable dt = new DataTable("tmp");
                dt.Load(reader);
                base.Close();
                return dt;
            }
            catch (Exception)
            {
                base.Close();
                return null;
            }
        }
        /// <summary>
        /// Thêm 1 dòng mới vào bảng tb_Fee
        /// </summary>
        /// <param name="name"></param>
        /// <param name="cost"></param>
        /// <returns></returns>
        public bool Insert(string name, long cost)
        {
            try
            {
                base.Open();
                base.Cmd.CommandText = string.Format("insert into tb_Fee(Name,Cost) values(N'{0}',{1})",name,cost);
                if (base.Cmd.ExecuteNonQuery() > 0)
                {
                    base.Close();
                    return true;
                }
                else
                {
                    base.Close();
                    return true;
                }
            }
            catch (Exception)
            {
                base.Close();
                return false;
            }
        }
        /// <summary>
        /// Cập nhật lại dữ liệu trong bảng tb_Fee
        /// </summary>
        /// <param name="id">id cần cập nhật</param>
        /// <param name="name"></param>
        /// <param name="cost"></param>
        /// <returns></returns>
        public bool Update(int id, string name, long cost)
        {
            try
            {
                base.Open();
                base.Cmd.CommandText = string.Format("update tb_Fee set Name=N'{0}', Cost={1} where Id={2}",name,cost,id);
                if (base.Cmd.ExecuteNonQuery() > 0)
                {
                    base.Close();
                    return true;
                }
                else
                {
                    base.Close();
                    return false;
                }
            }
            catch (Exception)
            {
                base.Close();
                return false;
            }
        }
        /// <summary>
        /// Xóa dữ liệu trong bảng tb_Fee
        /// </summary>
        /// <param name="id">id cần xóa</param>
        /// <param name="name"></param>
        /// <param name="cost"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            try
            {
                base.Open();
                base.Cmd.CommandText = string.Format("delete from tb_Fee where Id={0}",id);
                if (base.Cmd.ExecuteNonQuery() > 0)
                {
                    base.Close();
                    return true;
                }
                else
                {
                    base.Close();
                    return false;
                }
            }
            catch (Exception)
            {
                base.Close();
                return false;
            }
        }
    }
}
